﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Collections.ObjectModel;
using System.Threading;
using SLControlLibrary.Resources.Controls.Calendar.Common;
using SLControlLibrary.Resources.Controls.Calendar.ClassicCalendar;
using SLControlLibrary.Resources.Controls.Calendar.ClassicCalendar.Events;

namespace SLControlLibrary.Resources.Controls.Calendar.ClassicCalendar.BusinessEntities
{
    public class SelectedDatesCollection : ObservableCollection<DateTime>
    {
        #region Variables
        private Collection<DateTime> _addedItems;
        private ucCalendar _owner;
        #endregion

        #region Constructor
        public SelectedDatesCollection(ucCalendar owner)
        {
            _owner = owner;
            this._addedItems = new Collection<DateTime>();
        }
        #endregion

        #region Methods
        #region AddDateToList
        public void AddDateToList(DateTime Date)
        {
            this._addedItems.Clear();
            base.ClearItems();

            this.Add(Date);

            _owner.OnSelectedDatesCollectionChanged(new SelectionChangedEventArgs(this._owner._RemovedDates, this._addedItems));
            this._owner._RemovedDates.Clear();
        }
        #endregion

        #region AddRange
        public void AddRange(DateTime start, DateTime end)
        {
            DateTime? rangeStart;
            int increment = (DateTime.Compare(end, start) >= 0) ? 1 : -1;
            this._addedItems.Clear();

            rangeStart = start;

            if (this._owner._IsMouseSelection)
            {
                while (rangeStart.HasValue && DateTime.Compare(end, rangeStart.Value) != -increment)
                {
                    this.Add(rangeStart.Value);
                    rangeStart = Utility.AddDays(rangeStart.Value, increment);
                }
            }
            else
            {
                foreach (DateTime item in this)
                    this._owner._RemovedDates.Add(item);

                this.ClearInternal();

                while (rangeStart.HasValue && DateTime.Compare(end, rangeStart.Value) != -increment)
                {
                    this.Add(rangeStart.Value);
                    rangeStart = Utility.AddDays(rangeStart.Value, increment);
                }
            }

            _owner.OnSelectedDatesCollectionChanged(new SelectionChangedEventArgs(this._owner._RemovedDates, this._addedItems));
            this._owner._RemovedDates.Clear();
        }
        #endregion

        #region ClearItems
        protected override void ClearItems()
        {
            Collection<DateTime> addedItems = new Collection<DateTime>();
            Collection<DateTime> removedItems = new Collection<DateTime>();

            foreach (DateTime item in this)
                removedItems.Add(item);

            base.ClearItems();

            if (removedItems.Count != 0)
                _owner.OnSelectedDatesCollectionChanged(new SelectionChangedEventArgs(removedItems, addedItems));
        }
        #endregion

        #region ClearInternal
        public void ClearInternal()
        {
            base.ClearItems();
        }
        #endregion
        #endregion
    }
}
